package com.hospital.service.impl;

import com.hospital.config.SystemConfig;
import com.hospital.dao.*;
import com.hospital.dto.DignoseLogDto;
import com.hospital.dto.LogisticsStateDto;
import com.hospital.dto.LogisticsStateTjDto;
import com.hospital.dto.UserpayDto;
import com.hospital.entity.Admin;
import com.hospital.entity.Medicine;
import com.hospital.entity.SeeDoctor;
import com.hospital.entity.User;
import com.hospital.service.intf.UserService;
import com.hospital.util.PassUtils;
import com.hospital.util.StringUtil;
import com.hospital.vo.PageBean;
import com.hospital.vo.PriceVo;
import com.hospital.vo.R;
import com.hospital.vo.UserChartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author fxy
 * @date 2022/8/28
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserlogDao userlogDao;
    @Autowired
    private UserpayDao userpayDao;
    @Autowired
    private RegisterDao registerDao;
    @Autowired
    private MedpayDao medpayDao;
    @Autowired
    private MedicineDao medicineDao;
    @Autowired
    private SeedoctorDao seedoctorDao;
    //添加用户
    @Override
    public R add(User user) {
        if(userDao.add(user)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }
    @Override
    public R update(User user) {
        if(userDao.update(user)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


    @Override
    public R delete(int id) {
        if(userDao.delete(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R del(int[] id) {
        if(userDao.del(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBean selectPage(String name, int page, int limit) {

        if(StringUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }
        int start=(page-1)*limit;

        return PageBean.ok(userDao.selectCount(name),userDao.selectPage(name,start,limit));

    }

    @Override
    public R selectOne(User user, HttpSession session) {
        //1.校验参数
        if(StringUtil.isNotEmpty(user.getName(), user.getPassword())){
            //2.查询数据库
            User user1=userDao.selectOne(user);
            //3.验证是否存在用户
            if(user1!=null){
                //5.校验密码
                if(user1.getPassword().equals(PassUtils.aesenc(SystemConfig.PASS_KEY,user.getPassword()))){
                    //6.登录成功 存储到Session
                    session.setAttribute("user",user1);
                    //7.返回
                    return R.ok(user1);
                }
            }
        }
        //防止撞库
        return R.fail("亲，账号或密码错误");
    }

    @Override
    //用户买药
    public R updatesuplusTx(UserpayDto userpayDto) throws ParseException {

        //获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date(System.currentTimeMillis()));
        Date date = simpleDateFormat.parse(format);
        //判断用户余额
        if(userpayDto.getChange()>userDao.querysurplus(userpayDto.getUid()).getSurplus()){
            return R.fail("亲，余额不足请充值");
        }

        //判断药品库存
        if(medicineDao.queryStock(userpayDto.getMedid())<=0){
            return R.fail("亲，药品库存不足~~");
        }
//        System.out.println(userpayDto);
//        System.out.println("---------------");
        //添加一条用户看病记录
        if(seedoctorDao.addSeedoc(new SeeDoctor(userpayDto.getUid(),userpayDto.getEmpid(),userpayDto.getRegistertime(),userpayDto.getSymptom(),date))>0) {
         //更改用户余额
         if (userDao.updatesurplus(userpayDto.getUid(), (-1) * userpayDto.getChange()) > 0) {
             //添加用户账单历史记录
             if (userlogDao.addUserlog(userpayDto.getUid(), (-1) * userpayDto.getChange()) > 0) {
                 //添加用户买药记录
//                 System.out.println(date);
                 if (userpayDao.addmedpay(userpayDto.getUid(), userpayDto.getMedid(), userpayDto.getStype(),date) > 0) {
                     //改变药品记录
                     if (medpayDao.addmedlog(userpayDto.getMedid(), userpayDto.getChange()) > 0) {
                         //改变药品库存数
                         if (medicineDao.updatelog(userpayDto.getMedid(), -1) > 0) {
                             //改变用户挂号记录
                             if (registerDao.update(userpayDto.getUid()) > 0) {
                                 return R.ok();
                             }
                         }
                     }
                 }
             }
         }
     }
        return R.fail();

    }

    @Override
    //批量导入数据
    public int addUserBatch(List<User> users) {
        if(userDao.updateBatch(users)>0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public List<User> select() {
        return userDao.select();
    }

    @Override
    public R updateimg(Integer id, String url) {
        if(userDao.updateimg(id,url)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    //分页查询用户所有的诊断记录
    public PageBean selectdiglog(String symptom, int page, int limit,int id) {
        if(StringUtil.isNotEmpty(symptom)){
            symptom="%"+symptom+"%";
        }
        int start=(page-1)*limit;

        return PageBean.ok(userDao.queryCount(symptom,id),userDao.queryLog(symptom,start,limit,id));
    }

    @Override
    //查询用户所有的诊断记录
    public List<DignoseLogDto> selectdiglogall(String symptom) {
        return userDao.queryalllog();
    }

    @Override
    public R priceTj() {
        List<PriceVo> list = userDao.selectPrice();
        List<UserChartsVo> result = new ArrayList<>();
        list.forEach(s -> {
            Double d = s.getPrice()*(-1);
            result.add(new UserChartsVo(s.getUname(), d));
        });
        return R.ok(result);
    }



}
